package com.drx.service.service;

import com.drx.dao.TabRecordMapper;
import com.drx.dojo.TabRecord;
import com.drx.service.ReservationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


@Service
public class ReservationImpl implements ReservationService {
    @Autowired
    TabRecordMapper tabRecordMapper;

    @Override
    public List<TabRecord> reservatedSeat(Integer floarNum, String area, Integer beginTime, Integer overTime) {
        List<TabRecord> result=new ArrayList<TabRecord>();

        List<TabRecord> tabRecords = tabRecordMapper.selectByFloarNumAndArea(floarNum, area,new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
        //遍历tabRecords
        for (TabRecord tabRecord:tabRecords){
            //判断当前位置是否在所选时间段内
            if (tabRecord.getReservateBeginTime()>=overTime || tabRecord.getReservateOverTime()<=beginTime){
                //两段时间互补干扰，可以预约
            }else{
                //两段时间相互重叠，不可以预约，将不能预约的tabRecord装入resule中返回
                result.add(tabRecord);
            }
        }
        return result;
    }

    @Override
    public Integer beginReservation(Integer studentNum, Integer addressFloar, String addressArea, Integer addressNum, Integer beginTime, Integer overTime) {
        TabRecord tabRecord=new TabRecord();
        tabRecord.setUid(studentNum);
        tabRecord.setAddressFloar(addressFloar);
        tabRecord.setAddressArea(addressArea);
        tabRecord.setAddressNum(addressNum);
        tabRecord.setReservateBeginTime(beginTime);
        tabRecord.setReservateOverTime(overTime);
        tabRecord.setDay(new Date());
        tabRecord.setStatus("y");
        int result= tabRecordMapper.insert(tabRecord);
        return result;
    }

    @Override
    public TabRecord effectiveReservation(Integer uid, Date day) {
        //调用mapper的方法，看看能不能获取到数据
        TabRecord tabRecord = tabRecordMapper.selectByUidDay(uid, new SimpleDateFormat("yyyy-MM-dd").format(day));
        return tabRecord;
    }

    @Override
    public List<TabRecord> selectHistoryByStudentNum(Integer studentNum) {
        //调用mapper的方法，获取到所有的tabRecord对象
        List<TabRecord> tabRecords = tabRecordMapper.selectHistoryByStudentNum(studentNum);
        return tabRecords;
    }

    @Override
    public Integer endReservat(Integer studentNum) {
        //先调用effectiveReservation查询到数据
        TabRecord tabRecord = effectiveReservation(studentNum, new Date());
        tabRecord.setStatus("n");
        int i = tabRecordMapper.updateByPrimaryKey(tabRecord);
        return i;
    }

    @Override
    public Void checkExpiredReservat(Integer expiredTime) {
        //首先调用mapper的方法查找到当前过期时间和状态为'y'的所有TabRecord
        List<TabRecord> tabRecords = tabRecordMapper.selectByExpiredTime(expiredTime);
        for (TabRecord tabRecord:tabRecords){
            //遍历将所有的tabRecord的状态码设置为'n',并调用mapper文件进行修改
            tabRecord.setStatus("n");
            tabRecordMapper.updateByPrimaryKey(tabRecord);
        }
        return null;
    }
}
